package dictcn.com.control;

import com.weibo.sdk.android.Oauth2AccessToken;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


public class AccessSinaTokenKeeper {//保存新浪用户认证的oauth类
	
	private static final String PREFERENCES_NAME_SINA = "sinaToken";

	public static void keepSinaAccessToken(Context context, Oauth2AccessToken token) {	
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME_SINA, Context.MODE_PRIVATE);
		Editor editor = pref.edit();
		editor.putString("token", token.getToken());
		editor.putLong("expiresTime", token.getExpiresTime());
		editor.putString("refresh_token", token.getRefreshToken());
		editor.commit();
	}

	
	public static void clearSinaToken(Context context){
	    SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME_SINA, Context.MODE_PRIVATE);
	    Editor editor = pref.edit();
	    editor.clear();
	    editor.commit();
	}

	public static Oauth2AccessToken readSinaAccessToken(Context context){
		Oauth2AccessToken token = new Oauth2AccessToken();
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME_SINA, Context.MODE_PRIVATE);
		token.setToken(pref.getString("token", "-1"));
		token.setExpiresTime(pref.getLong("expiresTime", 0));
		token.setRefreshToken(pref.getString("refresh_token","-1"));
		return token;
	}
}
